<template>
	<view>
		<u-swipe-action :show="item.show" :index="item.appId" v-for="(item, index) in appList" :key="item.appId"
			@click="click" :options="options" style="border-bottom: 1rpx solid #C0C0C0;">
			<view style="display: flex;width: 100%;padding: 20rpx;">
				<view><img style="width: 80rpx;height: 80rpx;border-radius: 80rpx;" :src="item.appUserAvatar" alt="">
				</view>
				<view style="margin-left: 20rpx;">
					<view>{{item.appUserName}}</view>
					<view v-if="item.appApplicationReason!==null">申请理由：<span>{{item.appApplicationReason}}</span></view>
					<view style="font-size: smaller;color: #808080;margin-top: 10rpx;">{{item.createAt}}</view>
				</view>
			</view>
		</u-swipe-action>

		<!-- 拒绝申请弹出层 -->
		<u-popup v-model="appShow" mode="center" width="600">
			<view style="margin: 40rpx;">
				<h3 style="text-align: center;">拒绝原因</h3>
				<u-input style="height: 240rpx;margin-top: 20rpx;margin-bottom: 20rpx;" :border="true"
					v-model="judgeRequest.reason" type="textarea" label="拒绝原因" placeholder="请输入拒绝原因" />
				<u-button type="success" @click="refuse">提交</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orgId: null,
				appList: [],
				options: [{
						text: '同意',
						style: {
							backgroundColor: 'green'
						}
					},
					{
						text: '拒绝',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				],
				appShow: false,
				judgeRequest: {
					appId: null,
					reason: null,
					res: null
				}
			}
		},
		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			this.orgId = option.orgId;
			this.getOrgApplication()
		},
		methods: {
			// 拒绝申请
			refuse() {
				this.appShow = false;
				uni.showModal({
					title: '审批',
					content: '您确定拒绝该申请吗?',
					confirmTextColor: 'red',
					success: res => {
						if (res.confirm) {
							this.judege(this.judgeRequest.appId, false, this.judgeRequest.reason);
							this.judgeRequest.appId = null;
							this.judgeRequest.reason = null;
						} else {
							this.appShow = true
						}
					}
				})
			},
			click(appId, index1) {
				if (index1 == 0) {
					// 同意
					uni.showModal({
						title: '审批',
						content: '您确定同意该申请吗?',
						success: res => {
							if (res.confirm) {
								this.judege(appId, true, null);
							}
						}
					})
				} else {
					// 弹出层输入理由
					this.judgeRequest.appId = appId;
					this.appShow = true;
				}
			},
			// 审批学生入社
			judege(appId, res, reason) {
				this.$http.request({
					url: '/organizations/web/organization/judge',
					method: 'POST',
					data: {
						appId: appId,
						res: res,
						reason: reason
					}
				}).then(res => {
					res = res.data;
					if (res.result) {
						uni.showModal({
							content: res.msg,
							showCancel: false,
							success: res => {
								this.getOrgApplication()
							}
						});
					} else {
						uni.showModal({
							content: res.msg,
							showCancel: false
						});
					}
				})
			},
			getOrgApplication() {
				this.$http.request({
					url: `/organizations/web/organization/appList/${this.orgId}`
				}).then(res => {
					res = res.data;
					if (res.result) {
						this.appList = res.data;
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style>
</style>
